package com.selenium.base;

import java.io.FileInputStream;
import java.net.URL;
import java.util.Hashtable;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.internal.Locatable;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.events.internal.EventFiringMouse;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.selenium.MavenSelenium.MouseListener;


public class base {
	public static Properties props = null;
	public static Logger logs = null;
	public static Properties OR = null;
	public static boolean isBrowserOpened=false;
	public static base b;
	public static DesiredCapabilities dc=null;
	public static WebDriver driver=null;
	public static Hashtable<String, String> session;
	private base() 
	{
		try
		{
				
			logs = Logger.getLogger("devpinoyLogger");
			logs.debug("Loading Property files");
			
			props = new Properties();
			FileInputStream instream = new FileInputStream(System.getProperty("user.dir")+"//src//test//java//com//selenium//config//config.properties");
			props.load(instream);
			
			OR = new Properties();
			FileInputStream instreamor = new FileInputStream(System.getProperty("user.dir")+"//src//test//java//com//selenium//config//OR.properties");
			OR.load(instreamor);
			logs.debug("Loaded Property files successfully");
			session= new Hashtable<String, String>();
		}
		catch(Throwable t)
		{
			t.printStackTrace();
		}
	}
public static base getInstance() 
{
	if(b==null)
	{
		b= new base();
	}
	return b;
}
@Test
public void openBrowser(String browser)
{
	if(!isBrowserOpened)
	{	
		//String brow= props.getProperty(browser);
			if(browser.equals("firefox"))
			{
				dc = DesiredCapabilities.firefox();
				dc.setBrowserName("firefox");
				dc.setPlatform(org.openqa.selenium.Platform.ANY);
				/*driver = new FirefoxDriver();
				driver.manage().window().maximize();*/
				//driver= new EventFiringWebDriver(web_driver);
			}
			else if(browser.equals("chrome"))
			{
				dc = DesiredCapabilities.chrome();
				dc.setBrowserName("chrome");
				dc.setPlatform(org.openqa.selenium.Platform.ANY);
				/*driver = new InternetExplorerDriver();
				driver= new EventFiringWebDriver(web_driver);
				driver.manage().window().maximize();*/
			}
			else if(browser.equals("Chrome"))
			{
				dc = DesiredCapabilities.internetExplorer();
				dc.setBrowserName("iexplore");
				dc.setPlatform(org.openqa.selenium.Platform.WINDOWS);
				//dc.setCapability("jenkins.label", "redhat5 && amd64");
				/*System.setProperty("webdriver.chrome.driver",  "C:\\Manjusha\\Selenium\\ChromeDriver\\chromedriver.exe");
				driver = new ChromeDriver();*/
				
				//driver= new EventFiringWebDriver(web_driver);
			}
			if(driver==null)
			{
				try
				{
				driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dc);
				driver.manage().window().maximize();
				}
				catch(Throwable t)
				{
					logs.debug("driver having problem");
				}
				
			}
			isBrowserOpened= true;
	 }
	
	
	}

/*@DataProvider(parallel=true)
public Object[][] getData()
{
	Object[][] data = new Object[2][1];
	data[0][0] = "firefox";
	data[1][0] = "chrome";
	return data;
	
}*/
	public void menu(String header, String submenu)
	{
		
		Actions action = new Actions(driver);
		WebElement headerMenu= driver.findElement(By.xpath(OR.getProperty(header)));
		WebElement subMenu= driver.findElement(By.xpath(OR.getProperty(submenu)));
		
		action.moveToElement(headerMenu);
		action.click(headerMenu);
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		action.click(subMenu);
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
	}
	public boolean verifyTitle(String expectedValue)
	{
		try
		{
			String actualtitle= driver.getTitle();
			Assert.assertEquals(actualtitle, expectedValue);
			System.out.println("Title is matching");
		}
		catch(Throwable t)
		{
			logs.debug("Title do not match");
		   return false;
		}
		return true;
	}
	public String getText(String xP)
	{
		String text = driver.findElement(By.xpath(OR.getProperty(xP))).getText();
		System.out.println("The name is:" +text);
		return text;
	}
	public void implicitWait()
	{
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}
	public void gotoURL(String url)
	{
		driver.get(props.getProperty(url));
		
	}
	public void closeBrowser()
	{
		driver.close();
	}
	public void quitBrowser()
	{
		driver.quit();
	}
	public void text(String xP, String value)
	{
		try
		{
			driver.findElement(By.xpath(OR.getProperty(xP))).clear();
		driver.findElement(By.xpath(OR.getProperty(xP))).sendKeys(value);
		//driver.findElement(OR.getProperty(xP)).sendKeys(value);
		}
		catch(Throwable t)
		{
			t.printStackTrace();
			logs.debug("Text field not found");
		}
	
	}
	public void menuHeader(String xP)
	{
		try
		{
		driver.findElement(By.xpath(OR.getProperty(xP))).click();
		}
		catch(Throwable t)
		{
			t.printStackTrace();
			logs.debug("Menu not found");
		}
	}
	public void eventListener(String xP)
	{
		
		MouseListener listener = new MouseListener();
		EventFiringMouse mouse = new EventFiringMouse(driver, listener);
		driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
		new FluentWait<WebDriver>(driver)
		.withTimeout(50, TimeUnit.SECONDS)
		.pollingEvery(5, TimeUnit.SECONDS)
		.ignoring(NoSuchElementException.class).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(OR.getProperty(xP))));
		Locatable locate = (Locatable)driver.findElement(By.xpath(OR.getProperty(xP)));
		Coordinates coordinate = locate.getCoordinates();
		mouse.mouseMove(coordinate);
		mouse.click(coordinate);
		
	}
	public void eventMenu(String xheader, String xP)
	{
		driver.findElement(By.xpath(OR.getProperty(xheader))).click();
		
		MouseListener listener = new MouseListener();
		EventFiringMouse mouse = new EventFiringMouse(driver, listener);
		
		new FluentWait<WebDriver>(driver)
		.withTimeout(50, TimeUnit.SECONDS)
		.pollingEvery(5, TimeUnit.SECONDS)
		.ignoring(NoSuchElementException.class).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(OR.getProperty(xP))));
		Locatable locate = (Locatable)driver.findElement(By.xpath(OR.getProperty(xP)));
		Coordinates coordinate = locate.getCoordinates();
		mouse.mouseMove(coordinate);
		mouse.click(coordinate);
		
	}
	public void button(String xP)
	{
		try
		{
		driver.findElement(By.xpath(OR.getProperty(xP))).click();
		}
		catch(Throwable t)
		{
			t.printStackTrace();
			logs.debug("Button not found");
		}
			
	}
}
